package com.coding.utils;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.live.model.v20161101.*;
import com.coding.config.AcsClientConfig;
import com.coding.dto.RestBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.*;
import java.time.format.DateTimeFormatter;

@Component
public class LiveUtil {
    @Autowired
    private DefaultAcsClient acsClient;


    //获取正在推流的直播流
    public RestBean getAllLive() {
        DescribeLiveStreamsOnlineListRequest request = new DescribeLiveStreamsOnlineListRequest();
        request.setDomainName("codelove.icu");
        try {
            DescribeLiveStreamsOnlineListResponse response = acsClient.getAcsResponse(request);
            System.out.println( "--------------------------"+JSON.toJSONString(response) );

            return  RestBean.success("success",response);
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }

        return RestBean.failure(400,"获取失败");
    }


    //封禁某个用户的直播推流
    public RestBean banStream(String streamName, String BannedDuration) {
        ForbidLiveStreamRequest request = new ForbidLiveStreamRequest();
        request.setDomainName("codelove.icu");
        request.setAppName("DyApp");
        request.setLiveStreamType("publisher");
        request.setOneshot("no");

        request.setStreamName(streamName);
        //获取当前的UTC时间
        LocalDateTime banEndTime = calculateBanEndTime(BannedDuration);
        // 将LocalDateTime转换为系统默认时区的ZonedDateTime
        ZonedDateTime zonedDateTime = banEndTime.atZone(ZoneId.systemDefault());

        // 将ZonedDateTime转换为UTC时区
        ZonedDateTime utcDateTime = zonedDateTime.withZoneSameInstant(ZoneId.of("UTC"));

        // 定义所需格式的DateTimeFormatter
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");

        // 将UTC时间格式化为所需的字符串
        String utcDateTimeString = utcDateTime.format(formatter);
        //设置封禁恢复时间
        request.setResumeTime(utcDateTimeString);

        try {
            ForbidLiveStreamResponse response = acsClient.getAcsResponse(request);
            System.out.println("--------------------------"+JSON.toJSONString(response));
            return  RestBean.success("success",response);

        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
        return RestBean.failure(400,"封禁失败");

    }


    private LocalDateTime calculateBanEndTime(String duration) {
        //获取当前时间
        LocalDateTime localDateTime = LocalDateTime.now();

        switch (duration) {
            case "h":
                return  localDateTime.plusHours(1);
            case "d":
                return localDateTime.plusDays(1);
            case "M":
                return localDateTime.plusMonths(1);
            case "Y":
                return localDateTime.plusYears(1);
            default:
                return localDateTime.plusYears(100);
        }
    }

}
